layui.use(["layer", "laypage"], function () {
    const layer = layui.layer;
    const laypage = layui.laypage;

    var language = $("#infoTop").text().length == 4 ? "zh" : "en";
    var btnText = language == "zh" ? "编辑" : "Edit";
    /********************************************************************/
    if ($("#menuText").val() == "") {
        $("#menuBtn1").attr("disabled", true);
    }
    $("#menuText").keydown(function(event) {
        if (event.keyCode == 13)
            $("#menuBtn2").click();
    });
    $("#menuBtn1").click(function() {
        window.location.href = "markview";
    });
    $("#menuBtn2").click(function() {
        var name = $("#menuText").val();
        if (name.trim() != "")
            window.location.href = "markview?name=" + name;
    });
    /********************************************************************/
    var name = $("#menuText").val();
    $("#tab1 tbody tr").each(function(i) {
    	var id = $(this).attr("id");
        $(this).find("a").attr("target", "_blank");
        $(this).find("a").attr("href", "/survey/project/findinfo?id=" + id);
        /*************************************************/
        if (name.trim() != "") {
            const text = $(this).find("td:eq(1) a").text();
            const font = "<span>" + name + "</span>";
            const expr = new RegExp(name, "gm");
            const cont = text.replace(expr, font);
            $(this).find("td:eq(1) a").html(cont);
        }
        /*************************************************/
        $(this).find("td:eq(6)").css("cursor", "pointer");
        $(this).find("td:eq(6)").mouseenter(function(e) {
            $(this).css("background-color", "#FFE793");
            if ($(this).text() == 0)
                return false;
            var context = "";
            var data = Ajax("marklist", {id: id});
            for (var i = 0; i < data.length; i++) {
                context += "<tr>";
                context += "  <td>" + (i + 1) + "</td>";
                context += "  <td>" + data[i].score + "</td>";
                context += "  <td>" + data[i].date + "</td>";
                context += "  <td><input type='button' name='" + data[i].id + "'/></td>";
                context += "</tr>";
            }
            $("#show table tbody").html(context);
            $("#show").css("top", $(this).position().top);
            $("#show").css("left", $(this).position().left + 96);
            $("#show table tbody input").attr("value", btnText);
            $("#show").show();
        });
        $(this).find("td:eq(6)").mouseleave(function(e) {
            if (i % 2 == 0)
                $(this).css("background-color", "#FAFAFA");
            else
                $(this).css("background-color", "#EEEEEE");
            $("#show").hide();
        });
        /*************************************************/
        $(this).find("input:eq(0)").click(function() {
        	window.open("/survey/downfile?id=" + id);
        });
        $(this).find("input:eq(1)").click(function() {
            window.open("markitem?id=" + id);
        });
        $(this).click(function() {
            $("#tab1 tbody tr:even").find("td:eq(0)").css("background-color", "#FAFAFA");
            $("#tab1 tbody tr:odd").find("td:eq(0)").css("background-color", "#EEEEEE");
            $(this).find("td:eq(0)").css("background-color", "#FFD58D");
        });
    });
    $("#show table tbody").on("click", "input", function() {
    	window.open("editmark?id=" + $(this).attr("name"));
    })
    $("#show").mouseenter(function(e) {
        $(this).show();
    });
    $("#show").mouseleave(function(e) {
        $(this).hide();
    });

    /********************************************************************/
    laypage.render({
        elem: "page",
        curr: $("#page").data("p1"),
        count: $("#page").data("p2"),
        limit: 15,
    });
    $("#page a").on("click", function () {
        let page = $(this).text();
        if (page === "上一页")
            page = Number($("#page").data("p1") - 1);
        if (page === "下一页")
            page = Number($("#page").data("p1") + 1);
        location.href = "markview?name=" + name + "&page=" + page;
    });
    $(".layui-disabled").off("click");
    /*******************************************************************/
    /** 执行AJAX操作 */
    function Ajax(url, data) {
        var result = null;
        $.ajax({
            url: url,
            data: data,
            type: "post",
            async: false,
            datatype: "json",
            success: function(data) {
                result = data;
            }
        });
        return result;
    }
});
